package net.deuce.perf;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.beans.factory.InitializingBean;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.File;
import java.util.List;
import java.util.LinkedList;
import java.util.Iterator;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class Configuration implements InitializingBean {
    private File resultsDir;
    private File baseResultsDir;
    private String timestamp;
    private List<String> appServerLogs = new LinkedList<String>();
    private List<AppServerConfiguration> appServers = new LinkedList<AppServerConfiguration>();
    private List<AppServerConfiguration> dbServers = new LinkedList<AppServerConfiguration>();
    private ClientConfiguration client;
    private String timestampFormat = "yyyyMMdd-HHmmss";
    private Log log = LogFactory.getLog(getClass());

    public List<AppServerConfiguration> getDbServers() {
        //log.trace("");
        return dbServers;
    }

    public void setDbServers(List<AppServerConfiguration> dbServers) {
        //log.trace("");
        this.dbServers = dbServers;
    }

    public File getResultsDir() {
        //log.trace("");
        return resultsDir;
    }

    public void setResultsDir(File resultsDir) {
        //log.trace("");
        this.resultsDir = resultsDir;
    }

    public List<String> getAppServerLogs() {
        //log.trace("");
        return appServerLogs;
    }

    public void addAppServerLog(String s) {
        //log.trace("");
        appServerLogs.add(s);
    }

    public void setAppServerLogs(List<String> appServerLogs) {
        //log.trace("");
        this.appServerLogs = appServerLogs;
    }

    public List<AppServerConfiguration> getAppServers() {
        //log.trace("");
        return appServers;
    }

    public void setAppServers(List<AppServerConfiguration> appServers) {
        //log.trace("");
        this.appServers = appServers;
    }

    public void addAppServer(AppServerConfiguration appServer) {
        //log.trace("");
        appServers.add(appServer);
    }

    public ClientConfiguration getClient() {
        //log.trace("");
        return client;
    }

    public void setClient(ClientConfiguration client) {
        //log.trace("");
        this.client = client;
    }

    public void afterPropertiesSet() throws Exception {
        //log.trace("");
        if (baseResultsDir == null) {
            System.err.println("Configuration.baseResultsDir not set.");
            System.exit(-1);
        }
        DateFormat df = new SimpleDateFormat(timestampFormat);
        timestamp = df.format(new Date());
        resultsDir = new File(baseResultsDir, timestamp);
        resultsDir.mkdirs();
        if (!resultsDir.exists()) {
            System.err.println("resultsDir could not be created: " + resultsDir.getAbsolutePath());
        }
    }

    public File getBaseResultsDir() {
        //log.trace("");
        return baseResultsDir;
    }

    public void setBaseResultsDir(File baseResultsDir) {
        //log.trace("");
        this.baseResultsDir = baseResultsDir;
    }

    public String getTimestamp() {
        //log.trace("");
        return timestamp;
    }

    public String getTimestampFormat() {
        //log.trace("");
        return timestampFormat;
    }

    public void setTimestampFormat(String timestampFormat) {
        //log.trace("");
        this.timestampFormat = timestampFormat;
    }

}
